package com.example.hospitalapp;

import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

public class DoctorAppointmentListActivity extends AppCompatActivity {

    private ListView listViewAppointments;
    private ArrayList<Appointment> appointmentsList;
    private DoctorAppointmentAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_doctor_appointment_list);  // 布局文件

        listViewAppointments = findViewById(R.id.listViewAppointments);
        appointmentsList = new ArrayList<>();

        // 获取传递过来的医生ID
        int doctorId = getIntent().getIntExtra("doctorId", 0);
        if (doctorId != 0) {
            // 查询该医生的预约记录
            queryAppointments(doctorId);
        }
    }

    private void queryAppointments(int doctorId) {
        JSONObject params = new JSONObject();
        try {
            params.put("doctorId", doctorId);  // 传递医生ID查询
        } catch (Exception e) {
            e.printStackTrace();
        }

        ApiClient apiClient = new ApiClient(DoctorAppointmentListActivity.this);
        apiClient.postRequest("http://zhsj.autumnclouds.cn/appointment/query", params, new ApiClient.VolleyCallback() {
            @Override
            public void onSuccess(JSONObject response) {
                try {
                    int code = response.getInt("code");
                    if (code == 200) {
                        // 获取预约数据
                        JSONArray appointments = response.getJSONArray("data");
                        appointmentsList.clear();  // 清空现有的预约列表，确保不会重复

                        for (int i = 0; i < appointments.length(); i++) {
                            JSONObject appointmentData = appointments.getJSONObject(i);
                            Appointment appointment = new Appointment(
                                    appointmentData.getInt("appointmentId"),
                                    appointmentData.getString("appointmentTime"),
                                    appointmentData.getString("status"),
                                    appointmentData.getString("paymentStatus"),
                                    appointmentData.getInt("doctorId"),
                                    appointmentData.getInt("patientId"));
                            appointmentsList.add(appointment);
                        }

                        // 使用适配器展示预约数据
                        adapter = new DoctorAppointmentAdapter(DoctorAppointmentListActivity.this, appointmentsList);
                        listViewAppointments.setAdapter(adapter);
                    } else {
                        Toast.makeText(DoctorAppointmentListActivity.this, "未查询到预约信息", Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(DoctorAppointmentListActivity.this, "查询预约失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(String error) {

            }
        });
    }
}
